package by.myteam.gonki.gui.view;

import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JPanel;

import by.myteam.gonki.bo.CoordinatesObject;
import by.myteam.gonki.bo.IDrawable;
import by.myteam.gonki.bo.Road;
import by.myteam.gonki.bo.car.ECar;
import by.myteam.gonki.bo.car.SelectCar;
import by.myteam.gonki.states.mainstate.EGameStates;

@SuppressWarnings("serial")
public class SelectCarView extends JPanel {

	public SelectCarView() {
		this.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				for (ECar car : ECar.values()) {
					if (e.getX() >= ((CoordinatesObject) car.getCar()).getX()
							&& e.getX() <= ((CoordinatesObject) car.getCar()).getX() + car.getCar().getCarWidth()
							&& e.getY() >= ((CoordinatesObject) car.getCar()).getY()
							&& e.getY() <= ((CoordinatesObject) car.getCar()).getY() + car.getCar().getCarHeigth()) {
						SelectCar.INSTANCE.setCar(car.getCar());
						System.out.println(SelectCar.INSTANCE.getCar().nameCar());
						try {
							Thread.sleep(500);
							EGameStates.PLAY.getState().perform();				
						} catch (InterruptedException e1) {
							e1.printStackTrace();
						}
					}
				}

			}
		});
	}

	public void paint(Graphics g) {
		Road.INSTANCE.draw(g);
		for (ECar car : ECar.values()) {
			((IDrawable) car.getCar()).draw(g);
		}
	}

}
